package com.xckj.bigdata.demo.interceptor;

import com.xckj.bigdata.demo.entity.UserSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by 刘嘉民 on 2020/6/22 10:54
 */
@Component
public class SessionInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserSession userSession =(UserSession) request.getSession().getAttribute("userSession");
        if (userSession==null){
           request.setAttribute("msg","无权限请先登录");
           // 获取request返回页面到登录页
           request.getRequestDispatcher("/index").forward(request, response);
           return false;
       }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        UserSession userSession =(UserSession) request.getSession().getAttribute("userSession");
        System.out.println(request.getRequestURL());
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
